var tableElem = "LAY-home-page-list";
var formElem = "LAY-home-page-listform";
var tableToolsElem = "LAY-home-page-tools";
var formSubmitElem = "LAY-home-page-listform-submit";
var baseUrl = window.globalConfig.url_page_list

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , treeTable = layui.treeTable
        , form = layui.form;
    //页面管理
    var tableItem = treeTable.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , tree: {
            customName: {
                id: 'page_id',
                name: 'title'
            }
        }
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'page_id', width: 100, title: '页面id', sort: true}
            // , {field: 'nav_id', title: '导航id', minWidth: 100}
            , {field: 'title', title: '标题', minWidth: 200, edit: 'text'}
            // , {field: 'url', title: '路由', minWidth: 200, edit: 'text'}
            // , {field: 'urlrewriter', title: '自定义URL', minWidth: 200, edit: 'text'}
            , {
                field: 'is_show', title: '是否显示', templet: '#ID-table-page-is_show-switch'
            }
            // , {field: 'urlrewriter', title: 'url重写'}
            , {field: 'pagetitle', title: 'SEO标题', edit: 'text'}
            , {field: 'pagekeywords', title: 'SEO关键词', edit: 'text'}
            , {field: 'pagedesc', title: 'SEO描述', edit: 'text'}
            // , {field: 'order_id', title: '排序', edit: 'text'}
            // , {field: 'url', title: '网页路由'}
            ,
            {
                field: 'cmsList', title: "片段数量", templet: function (d) {
                    return d.part.length
                }
            }
            , {title: '操作', minWidth: 280, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'}
    });

    var active = {
        //添加
        add: function (othis) {
            admin.popup({
                title: '添加页面'
                , area: ['550px', '750px']
                , id: 'LAY-popup-content-add'
                , success: function (layero, index) {
                    view(this.id).render('home/page/listform').done(function () {
                        form.render(null, `${formElem}`);

                        //提交
                        form.on(`submit(${formSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: window.globalConfig.url_page_list,
                                type: "POST",
                                data: field,
                                success: function (res) {
                                    common.ctmMsg(res.code, res.msg)
                                    tableItem.reloadData(); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            })
                        });
                    });
                }
            });
        },
        editFooter(othis) {
            location.hash = '/home/page/footerEdit'
        },
        asyncNavigate(othis) {
            common.ctmRequest(`${baseUrl}/create`, [], 'GET', tableElem, true)
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除页面
            layer.confirm('确定删除此页面？', function (index) {
                admin.req({
                    url: window.globalConfig.url_page_list + "/" + data.page_id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        layer.close(index);
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {// 修改页面
            admin.popup({
                title: '编辑页面'
                , area: ['550px', '750px']
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {

                    admin.req({
                        url: window.globalConfig.url_page_list + "/" + data.page_id,
                        type: "GET",
                        success: (res) => {
                            data = res.data
                            view(this.id).render('home/page/listform', data).done(function () {
                                form.render(null, formElem);

                                setTimeout(() => {
                                    form.val(formElem, {
                                        nav_id: data.nav_id,
                                    })
                                }, 1000)

                                //提交
                                form.on(`submit(${formSubmitElem})`, function (data) {
                                    var field = data.field; //获取提交的字段

                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    // 修改页面的请求
                                    admin.req({
                                        url: window.globalConfig.url_page_list + "/" + field.page_id,
                                        type: "PUT",
                                        data: field,
                                        success: function (res) {
                                            common.ctmMsg(res.code, res.msg)
                                            tableItem.reloadData(); //重载表格
                                            layer.close(index); //执行关闭
                                        }
                                    })
                                });
                            });
                        }
                    })
                }
            });
        } else if (obj.event === 'addChild') {
            admin.popup({
                title: '添加子页面'
                , area: ['550px', '750px']
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {
                    admin.req({
                        url: baseUrl + "/" + data.page_id,
                        type: "GET",
                        success: (res) => {
                            let $parentId = res.data.page_id
                            data = {
                                "navName": res.data.navName,
                                "nav_id": res.data.nav_id,
                            };
                            console.log(data)
                            view(this.id).render('home/page/listform', data).done(function () {
                                form.render(null, formElem);

                                setTimeout(() => {
                                    form.val(formElem, {
                                        nav_id: data.nav_id,
                                    })
                                }, 1000)

                                //提交
                                form.on(`submit(${formSubmitElem})`, function (data) {
                                    var field = data.field; //获取提交的字段
                                    field.parent_id = $parentId
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    // 修改页面的请求
                                    admin.req({
                                        url: baseUrl,
                                        type: "POST",
                                        data: field,
                                        success: function (res) {
                                            common.ctmMsg(res.code, res.msg)
                                            tableItem.reloadData(); //重载表格
                                            layer.close(index); //执行关闭
                                        }
                                    })
                                });
                            });
                        }
                    })
                }
            });

        } else if (obj.event === 'copy') {
            layer.confirm('确定复制此页面？', function (index) {
                var field = data
                field.title = field.title + "_copy"
                field.action = 'copy'
                admin.req({
                    url: baseUrl,
                    type: "POST",
                    data: field,
                    success: function (res) {
                        common.ctmMsg(res.code, res.msg)
                        if (res.code === 0) {
                            tableItem.reloadData()
                        }
                    }
                })
            });

        }
    });


    // 表格文本修改
    common.tableTextEditor(tableElem, baseUrl, 'page_id')

    // 表格的切换器修改
    common.tableSwitchEditor('page-is_show-status', baseUrl)

});